﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Register";
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        LblError.Visible = false;        

        if (!FormValid())
        {
            LblError.Visible = true;
            return;
        }
        var email = TxtEmail.Text.Trim();
        var username = TxtUsername.Text.Trim();
        var pass = TxtPassword.Text.Trim();
        PomplingCustomers c = new PomplingCustomers() { Email = email, PomplingPassword = pass, UserName = username };
        c.Save();
        PomplingCustomers customer = PomplingCustomers.CustomerSelect(c.PomplingCustomerID);

        Response.Redirect("admin.aspx?c=" + customer.PomplingCustomerID);
    }

    private bool FormValid()
    {
        var email = TxtEmail.Text.Trim();
        var username = TxtUsername.Text.Trim();
        var pass = TxtPassword.Text.Trim();
        
        if (username.Length < 3 || username.Length > 29 )
        {            
            if (username.Length < 3)
                LblError.Text = "Username must be more than 2 characters.";
            else
                LblError.Text = "Username must be less than 30 characters";
            
            return false;
        }
        if (PomplingCustomers.UsernameUsed(username))
        {
            LblError.Text = "Username already in use.";
            return false;
        }
        if (pass.Length < 5 || pass.Length > 16)
        {
            LblError.Text = "Password must be more than 5 and less than 16 charcters.";
            return false;
        }
       
        if (email.Length == 0)
        {
            LblError.Text = "Please supply a valid email.";
            return false;
        }
        if (!email.Contains('@') && !email.Contains('.'))
        {
            LblError.Text = "Please supply a valid email.";
            return false;
        }
       
        if (PomplingCustomers.EmailUsed(email))
        {
            LblError.Text = "Email already in use.";   
            return false;
        }
       
        return true;
    }
}